package com.mmlc.meimei.lichengkongjian.classify;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterFragment;
import com.mmlc.inter.ViewPagerFragmentAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.CategoryListRequestFilter;
import com.mmlc.network.responsebean.CategoryListResponseBean;
import com.mmlc.utils.DimonsUtil;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.Indicator;

import java.util.ArrayList;
import java.util.List;

public class ClassifyFragment extends InterFragment {

    @Override
    protected int initResourceID() {
        return R.layout.fragment_classify;
    }

   /* ListView list_classify;
    List<CategoryListResponseBean.DataBean.ListBean> datas = new ArrayList<>();
    CategoryListResponseBean  result;
    ClassifyAdapter adapter;*/

    Indicator indicator_v;

    ViewPager vp;

    @Override
    protected void initView() {

        indicator_v = ViewUtils.getView(view, R.id.indicator_v);
        vp = ViewUtils.getView(view, R.id.container);
        indicator_v.setBottomHeight(DimonsUtil.dp2px(getResources().getDimensionPixelSize(R.dimen.x3),getActivity()));
        indicator_v.setExpandLength(getResources().getDimensionPixelSize(R.dimen.x75));
     /*   indicator_v.setIndexChangeListener(new Indicator.onIndexChangeListener() {
            @Override
            public void onIndexSelected(int index) {
                datas.clear();
                datas.addAll(result.getData().get(index).getList());
                adapter.notifyDataSetChanged();
            }
        });
        adapter = new ClassifyAdapter(context, datas, list_classify);
        list_classify.setAdapter(adapter);*/
//        adapter.setListener(new ClassifyAdapter.Listener() {
//            @Override
//            public void extend(int position) {
//                for (int i = 0; i < list_classify.getChildCount(); i++) {
//                    ExpendClassify expend = (ExpendClassify) list_classify.getChildAt(i);
//                    LogUtils.e(TAG, "list:" + i + "/" + position + "/" + (expend == null));
//                    if (i + list_classify.getFirstVisiblePosition() != position && expend != null && expend.isExtend())
//                        expend.toExpend(false);
//                }
//            }
//        });
    }

    @Override
    protected void initInfo() {
        super.initInfo();

        CategoryListRequestFilter filter = new CategoryListRequestFilter((BaseParentActivity) getActivity());
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CategoryListResponseBean>() {
            @Override
            public void onSuccess(CategoryListResponseBean result) {
                initCategoryList(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
            }
        });
    }

    /**
     * 初始化分类列表数据
     *
     * @param result
     */
    private void initCategoryList(CategoryListResponseBean result) {
        List<String> titles = new ArrayList<>();
        List<Fragment> fragments=new ArrayList<>();
        for (int i=0;i<result.getData().size();i++) {
            titles.add(result.getData().get(i).getName());
            ClassifyUiFragment fragment = new ClassifyUiFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable("data", result);
            bundle.putInt("index", i);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        indicator_v.setTitles(titles);
        vp.setAdapter(new ViewPagerFragmentAdapter(getChildFragmentManager(), fragments));
        indicator_v.setmViewPager(vp,0);
    }

}
